package org.example.Controller;

import lombok.extern.slf4j.Slf4j;
import org.example.Pojo.Result;
import org.example.Pojo.Student;
import org.example.Server.StudentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class StudentController {

    @Autowired
    private StudentServer studentServer;


    /*
     * 学员列表查询
     * */
    @GetMapping("/students")
    public Result getStudents(String name, Integer degree, Integer clazzId,
                              @DefaultValue("1") Integer page,
                              @DefaultValue("10") Integer pageSize) {
        log.info("getStudents");
        log.info("name:{}, degree:{}, clazzId:{}", name, degree, clazzId);
        log.info("page:{}, pageSize:{}", page, pageSize);
        List<Student> list=studentServer.getStudents(name,degree,clazzId,page,pageSize);
        return Result.success(list);
    }

    /*
    * 添加学生
    * */
    @PostMapping("/students")
    public Result addStudent(@RequestBody Student student) {
        log.info("addStudent");
        log.info("student:{}", student);
        studentServer.add(student);
        return Result.success();
    }

    /*
    * 根据Id查询学生
    * */
    @GetMapping("/students/{id}")
    public Result getStudentById(@PathVariable Integer id) {
        log.info("getStudentById");
        log.info("id:{}", id);
        Student student=studentServer.getStudentById(id);
        return Result.success(student);
    }

    /*
     * 更新学生信息
     * */
    @PutMapping("/students")
    public Result updateStudent(@RequestBody Student student) {
        log.info("updateStudent");
        log.info("student:{}", student);
        studentServer.updateStudent(student);
        return Result.success();
    }


    /*
     * 删除学生
     * */
    @DeleteMapping("/students/{ids}")
    public Result deleteStudent(@PathVariable List<Integer> ids) {
        log.info("deleteStudent");
        log.info("ids:{}", ids);
        studentServer.deleteStudent(ids);
        return Result.success();
    }


    /*
    * 违纪处理
    * */
    @PutMapping("/students/violation/{id}/{score}")
    public Result violationStudent(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("violationStudent");
        log.info("id:{}, score:{}", id, score);
        studentServer.violationStudent(id,score);
        return Result.success();
    }
}
